#include "Set.h"
void InitSet(Set *set, char *arr)
{
    for (int i = 0; i < SIZE; i++)
    {
        set->array[i].data_ = arr[i];
        set->array[i].parent_ = -1;
    }
    set->n_ = SIZE;
}
// 找x对应的下标
int FindIndex(Set *set, char x)
{
    int i = 0;
    for (; i < SIZE; i++)
    {
        if (set->array[i].data_ == x)
            break;
    }
    return i;
}
// 找x所在的根节点--version1
// int Find(Set *set, char c)
// {
//     int i = FindIndex(set, c);
//     while (set->array[i].parent_ >= 0)
//     {
//         i = set->array[i].parent_;
//     }
//     return i;
// }
//--version2
int Find(Set *set, char c)
{
    int i = FindIndex(set, c);
    int root = i;
    while (set->array[root].parent_ >= 0)
    {
        root = set->array[root].parent_;
    }
    while (set->array[i].parent_ >= 0)
    {
        int tmp = set->array[i].parent_;
        set->array[i].parent_ = root;
        i = tmp;
    }
    return root;
}
void Union(Set *set, char x1, char y1)
{
    int i = Find(set, x1);
    int j = Find(set, y1);

    if (i == j)
        return;
    if (set->array[i].parent_ > set->array[j].parent_)
    {
        set->array[j].parent_ += set->array[i].parent_;
        set->array[i].parent_ = j;
    }
    else
    {
        set->array[i].parent_ += set->array[j].parent_;
        set->array[j].parent_ = i;
    }
}

void Print(Set *set)
{
    for (int i = 0; i < SIZE; i++)
    {
        printf("%c[%d] ", set->array[i].data_, set->array[i].parent_);
    }
    printf("\n");
}